//
//  EndTripViewCtl.swift
//  WNKP
//
//  Created by iMac on 2018/3/5.
//  Copyright © 2018年 ZCZ. All rights reserved.
//

import UIKit

let App_Width      =    UIScreen.main.bounds.width
let App_Height     =    UIScreen.main.bounds.height
let iPhoneX = (App_Width == 375) && (App_Height == 812) ? true : false
let Nav_Height     :    CGFloat = iPhoneX ? 88 : 64
let Tabbar_Height  :    CGFloat = 49
let Nav_StatusBar_Height : CGFloat = iPhoneX ? 44 : 20


class EndTripViewCtl: UIViewController, UIScrollViewDelegate {
    
    var scrollView: UIScrollView!
    private var carsCount : CGFloat = 3 //数组长度
    var value:Int = 1  // 当前页面
    override func viewDidLoad() {  super.viewDidLoad() ;  configUI()  }
    
    
    
    @IBOutlet weak var firstBtn: UIButton!
    @IBOutlet weak var secondBtn: UIButton!
    @IBOutlet weak var thirdBtn: UIButton!
    @IBOutlet weak var forthBtn: UIButton!
    

    
    
    @IBAction func allaction(_ sender: UIButton) {
     
            if sender == firstBtn {
                value = 1
                let  xValue: CGFloat = (CGFloat(value) - 1) * (App_Width - 20)
                var offset = scrollView.contentOffset
                offset.x = xValue
                scrollView.setContentOffset(offset, animated: true)
            }
            else if sender == secondBtn {
                value = 2

                let  xValue: CGFloat = (CGFloat(value) - 1) * (App_Width - 20)
                var offset = scrollView.contentOffset
                offset.x = xValue
                scrollView.setContentOffset(offset, animated: true)

            }
            else if sender == thirdBtn {
                value = 3
                let  xValue: CGFloat = (CGFloat(value) - 1) * (App_Width - 20)
                var offset = scrollView.contentOffset
                offset.x = xValue
                scrollView.setContentOffset(offset, animated: true)

            }
            else if sender == forthBtn {
                value = 4
                let  xValue: CGFloat = (CGFloat(value) - 1) * (App_Width - 20)
                var offset = scrollView.contentOffset
                offset.x = xValue
                scrollView.setContentOffset(offset, animated: true)
        }
    }
    
    
}

extension EndTripViewCtl {
    
    private func configUI() {
        
        configContentView()
        
    }
    
    
    func configContentView() {
        
        scrollView = UIScrollView.init(frame: CGRect(x: 10, y: 100, width: App_Width - 20, height: 290))
        
        scrollView.contentSize = CGSize(width:(App_Width - 20) * carsCount, height: 290)
        
        
        
        scrollView.contentOffset =  CGPoint(x: 0, y: 0)
        
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        
        scrollView.bounces = true
        scrollView.isPagingEnabled = true
        
        //         循环添加view
        
        for i in 1...(Int(carsCount))
        {
            print("i===\(i)")
            
            let  xValue: CGFloat = (CGFloat(i) - 1) * (App_Width - 20)
            let myAlert = UseCarView(frame: CGRect(x: xValue , y: 0 , width: App_Width - 20 , height: 290))
            
            myAlert.pageValue = i
            
            myAlert.ButtonCallBack = {(type) -> () in
                
                self.value = myAlert.pageValue
                print(myAlert.pageValue)
                
                switch type {
                case 1:
                    print("点击事件")
                default:
                    print("Nothing")
                }
            }
            scrollView .addSubview(myAlert)
        }
        
        view.addSubview(scrollView)
        
        
        let buttonView = ButtonAutoView(frame: CGRect(x: 16, y: scrollView.frame.maxY - Nav_Height + Nav_StatusBar_Height - 30, width: App_Width - 32, height: 61))
        buttonView.ButtonCallBack = { (type) -> () in
            switch type {
            case 1:
                print("green click")
                let value = CGFloat(self.scrollView.contentOffset.x) / CGFloat((App_Width - 20))
                print(value)
            default:
                print("Nothing")
            }
            
        }
        
        view.addSubview(buttonView)
    }

    

    
        
    
        
       
        

    
        
  

}

